package algotithm.weekendrace.biweeklycontest45.test2;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/6-22:20
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {2,-5,1,-4,3,-2};
//        int[] nums = {1,-3,2,3,-4};
        int sum = solution.maxAbsoluteSum(nums);
        System.out.println(sum);

    }


    public int maxAbsoluteSum(int[] nums) {
        int sum = 0;
        int maxsum = 0;
        int minsum = 0;
        int res = 0;
        for(int i=0;i<nums.length;i++) {
            sum += nums[i];
            if(maxsum < sum) {
                maxsum = sum;
            }
            if(minsum > sum) {
                minsum = sum;
            }
            if(Math.abs(maxsum) > res) {
                res = Math.abs(maxsum);
            }
        }
        res = Math.max(Math.abs(maxsum - minsum), res);
        return res;
    }

}
